﻿public class C文件操作
        {
            public static bool F运行程序或文件(string _filepath)
            {
                try
                {
                    System.Diagnostics.Process.Start(_filepath);
                }
                catch
                {
                    System.Console.Write("\nSorry,运行失败：\n\n\"" + _filepath);
                }

                return true;
            }

            public static string F使路径合法化(string _filepath)
            {
                string _root = _filepath.Substring(0, _filepath.LastIndexOf("\\"));
                string 文件名不含后缀 = CBase.C主文件类.C文件操作.F提取文件名(_filepath, false);
                string 文件名包含后缀 = CBase.C主文件类.C文件操作.F提取文件名(_filepath, true);

                string 新的文件名不含后缀 = 文件名不含后缀;
                string[] _ichars = new string[] { "\\", "/", ":", "*", "?", "\"", "<", ">", "|", "&lt;", "&gt;", "&quot;" };
                foreach (string ichar in _ichars)
                    新的文件名不含后缀 = 新的文件名不含后缀.Replace(ichar, "");

                string _fileName = 文件名包含后缀.Replace(文件名不含后缀, 新的文件名不含后缀);

                //去除非法字符



                string _newPath = _root + "\\" + _fileName;


                return _newPath;

            }

            public static string F使目录合法化(string root)
            {
                
                if (root.Substring(root.Length - 1, 1) == "\\")
                {
                    return root;
                }
                else
                {
                    return root + "\\";
                }


            }

            public static string F根据网址重命名(string url)
            {
                string 新的文件名不含后缀 = url;
                string[] _ichars = new string[] { "\\", "/", ":", "*", "?", "\"", "<", ">", "|", "&lt;", "&gt;", "&quot;" };
                foreach (string ichar in _ichars)
                    新的文件名不含后缀 = 新的文件名不含后缀.Replace(ichar, "");

                return 新的文件名不含后缀;
            }

            public static bool F删除文件(string 要删除的路径)
            {
                string path = 要删除的路径;
                try
                {
                    File.Delete(path);

                    //Console.WriteLine("{0} was successfully deleted.", path);
                }
                catch
                {
                    return false;
                }
                return true;
            }

            public static bool F移动文件(string 原始路径, string 目标路径)
            {

                try
                {
                    File.Move(原始路径, 目标路径);
                    Console.WriteLine("{0} was successfully Moved.", 原始路径);
                }
                catch
                {
                    return false;
                }
                return true;
            }

            public static bool F删除目录(string 要删除的路径)
            {
                string path = 要删除的路径;
                try
                {
                    DirectoryInfo di = new DirectoryInfo(path);
                    di.Delete(true);
                }
                catch
                {
                    return false;
                }
                return true;
            }

            public static bool F复制文件(string 源文件位置, string 目标文件位置, bool 是否覆盖原有文件)
            {
                try
                {
                    File.Copy(源文件位置, 目标文件位置, 是否覆盖原有文件);

                    // Ensure that the target does not exis.


                    Console.WriteLine("{0} was successfully copyed.", 源文件位置);
                }
                catch (Exception e)
                {
                    Console.WriteLine("The process failed: {0}", e.ToString());
                }

                return true;
            }

            public static string F获取程序所在目录()
            {
                string rStr = System.Reflection.Assembly.GetExecutingAssembly().Location;
                int tindex = rStr.LastIndexOf(@"\");
                rStr = rStr.Substring(0, tindex + 1);
                return rStr;
            }

            //BUG说明：如文件名为：aaa.txt.rar则提取的文件名为：aaa而非aaa.txt[已修正了]
            public static string F提取文件名(string filepath, bool 是否包括后缀)
            {
                string curname = filepath.Substring(filepath.LastIndexOf("\\") + 1, filepath.Length - filepath.LastIndexOf("\\") - 1);
                if (是否包括后缀)
                {
                    return curname;
                }
                else
                {
                    if (curname.IndexOf('.') > 0)
                    {
                        string noHz = curname.Substring(0, curname.LastIndexOf('.'));
                        return noHz;
                    }
                    else
                        return curname;
                }

            }

            public static string F根据文件名提取文件目录名(string filepath)
            {
                string curname = filepath.Substring(0, filepath.LastIndexOf("\\"));
                return curname;
            }

            public static string F提取网页文件名(string urlpath, bool 是否包括后缀)
            {
                string curname = urlpath.Substring(urlpath.LastIndexOf("/") + 1, urlpath.Length - e.LastIndexOf("/") - 1);
                if (是否包括后缀)
                {
                    return curname;
                }
                else
                {
                    if (curname.IndexOf('.') > 0)
                    {
                        string noHz = curname.Substring(0, curname.LastIndexOf('.'));
                        return noHz;
                    }
                    else
                        return curname;
                }

            }

            public static string F原名的基础上改名(string filepath, string 前面添加的部分)
            {


                try
                {
                    string curname = filepath.Substring(filepath.LastIndexOf("\\") + 1, filepath.Length - filepath.LastIndexOf("\\") - 1);
                    string rStr = filepath.Replace(curname, 前面添加的部分 + curname);
                    return rStr;
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine("Error:" + ex.Message);
                    return "";
                }

            }

            public static long F获取目录大小(string target)
            {
                DirectoryInfo d = new DirectoryInfo(target);
                return F获取目录大小(d);
            }

            public static long F获取目录大小(DirectoryInfo d)
            {


                long Size = 0;
                // Add file sizes.
                FileInfo[] fis = d.GetFiles();
                foreach (FileInfo fi in fis)
                {
                    Size += fi.Length;
                }
                // Add subdirectory sizes.
                DirectoryInfo[] dis = d.GetDirectories();
                foreach (DirectoryInfo di in dis)
                {
                    Size += F获取目录大小(di);
                }
                return (Size);
            }

            //目录拷贝类
            public class FileExec
            {

                public FileExec()
                {

                }

                private static void CopyDirectory(DirectoryInfo OldDirectory, DirectoryInfo NewDirectory)
                {
                    string NewDirectoryFullName = NewDirectory.FullName + @"\" + OldDirectory.Name;

                    if (!Directory.Exists(NewDirectoryFullName))
                        Directory.CreateDirectory(NewDirectoryFullName);

                    FileInfo[] OldFileAry = OldDirectory.GetFiles();
                    foreach (FileInfo aFile in OldFileAry)
                        File.Copy(aFile.FullName, NewDirectoryFullName + @"\" + aFile.Name, true);

                    DirectoryInfo[] OldDirectoryAry = OldDirectory.GetDirectories();
                    foreach (DirectoryInfo aOldDirectory in OldDirectoryAry)
                    {
                        DirectoryInfo aNewDirectory = new DirectoryInfo(NewDirectoryFullName);
                        CopyDirectory(aOldDirectory, aNewDirectory);
                    }
                }

                //CopyDirectory
                public static void CopyDirectory(string OldDirectoryStr, string NewDirectoryStr)
                {
                    DirectoryInfo OldDirectory = new DirectoryInfo(OldDirectoryStr);
                    DirectoryInfo NewDirectory = new DirectoryInfo(NewDirectoryStr);
                    FileExec.CopyDirectory(OldDirectory, NewDirectory);
                }

                //DelDirectory
                public static void DelDirectory(string OldDirectoryStr)
                {
                    DirectoryInfo OldDirectory = new DirectoryInfo(OldDirectoryStr);
                    OldDirectory.Delete(true);
                }

                //CopyDirectory And DelDirectory
                public static void CopyAndDelDirectory(string OldDirectory, string NewDirectory)
                {
                    CopyDirectory(OldDirectory, NewDirectory);
                    DelDirectory(OldDirectory);
                }

            }

            //转为UTF8编码
            public static bool F默认编码转为UTF8编码(string 载入位置, string 输出位置)
            {
                try
                {
                    string readText = File.ReadAllText(载入位置.Trim(), System.Text.Encoding.Default);
                    File.WriteAllText(输出位置.Trim(), readText, System.Text.Encoding.UTF8);
                    return true;
                }
                catch
                {
                    System.Console.WriteLine("程序在：默认编码转为UTF8编码时出现错误：");
                    System.Console.WriteLine("不能处理文件" + 载入位置.Trim());
                    return false;
                }

            }

            //转为UTF8编码
            public static bool F默认编码转为其它编码(string 载入位置, string 输出位置)
            {
                try
                {
                    string readText = File.ReadAllText(载入位置.Trim(), System.Text.Encoding.UTF8);
                    File.WriteAllText(输出位置.Trim(), readText, System.Text.Encoding.GetEncoding("GB2312"));
                    return true;
                }
                catch
                {
                    System.Console.WriteLine("程序在：默认编码转为UTF8编码时出现错误：");
                    System.Console.WriteLine("不能处理文件" + 载入位置.Trim());
                    return false;
                }

            }

            //编码的批量转换
            //输入与输出的目录名应不包括“\"
            public static bool F批量转为UTF8编码(string 载入目录, string 输出目录, string 新的后缀名)
            {
                ArrayList waitConvAL = new ArrayList();

                waitConvAL = CBase.C主文件类.C获取.F获取所有文件路径列表(载入目录.Trim());

                //循环处理
                foreach (string tstr in waitConvAL)
                {
                    //重定义输出位置，即包括新后缀名的绝对路径
                    string newpath = 输出目录.Trim() + "\\" + CBase.C主文件类.C文件操作.F提取文件名(tstr, false) + 新的后缀名;

                    F默认编码转为UTF8编码(tstr, newpath);

                    System.Console.WriteLine(tstr);
                }

                return true;

            }

            public static bool F批量转为其它编码(string 载入目录, string 输出目录, string 新的后缀名)
            {
                ArrayList waitConvAL = new ArrayList();

                waitConvAL = CBase.C主文件类.C获取.F获取所有文件路径列表(载入目录.Trim());

                //循环处理
                foreach (string tstr in waitConvAL)
                {
                    //重定义输出位置，即包括新后缀名的绝对路径
                    string newpath = 输出目录.Trim() + "\\" + CBase.C主文件类.C文件操作.F提取文件名(tstr, false) + 新的后缀名;

                    F默认编码转为其它编码(tstr, newpath);

                    System.Console.WriteLine(tstr);
                }

                return true;

            }

            public static bool F文件编码相同的文件批量合并(string 最终文件绝对路径, string 待合并的文件目录)
            {
                //准备文件

                System.Console.WriteLine("UTF8文件批量合并：");

                if (!File.Exists(最终文件绝对路径))
                {
                    // Create a file to write to.
                    System.Console.Write("\n文件： \"" + 最终文件绝对路径 + "\" 不存在，正在创建...");
                    string createText = Environment.NewLine;
                    File.WriteAllText(最终文件绝对路径.Trim(), createText, Encoding.UTF8);
                    System.Console.Write("　　　　[ OK ]\n");
                }
                else
                {
                    System.Console.Write("\n文件：" + 最终文件绝对路径 + "已存在");
                }



                //合并文件

                ArrayList waitConvAL = new ArrayList();
                waitConvAL = CBase.C主文件类.C获取.F获取所有文件路径列表(待合并的文件目录.Trim());

                int 待处理的文件总数 = waitConvAL.Count;
                int 已处理个数 = 0;
                //循环处理
                foreach (string tstr in waitConvAL)
                {
                    已处理个数++;
                    System.Console.Write("\n正在处理第 " + 已处理个数 + "/" + 待处理的文件总数 + " 个文件：    " + tstr);
                    string curFile = Environment.NewLine + File.ReadAllText(tstr);
                    File.AppendAllText(最终文件绝对路径, curFile);
                    System.Console.Write("       [ OK ]");
                }

                return true;

            }

            public static ArrayList F获取所有编码列表()
            {
                ArrayList rAL = new ArrayList();
                rAL.Add("Default");
                foreach (EncodingInfo tec in Encoding.GetEncodings())
                {
                    string tstr = tec.Name.ToString() + "     " + tec.DisplayName.ToString();
                    rAL.Add(tstr);
                }
                return rAL;
            }

            public static Encoding F文本转编码(string 编码类型)
            {
                foreach (EncodingInfo tec in Encoding.GetEncodings())
                {
                    if (编码类型.Equals(tec.Name.ToString() + "     " + tec.DisplayName.ToString()))
                        return tec.GetEncoding();

                    if (编码类型.Equals(tec.Name.ToString()))
                        return tec.GetEncoding();
                }
                return Encoding.Default;
            }


        }